package com.example.multithread;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.multithread.databinding.ActivityMainBinding;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

    // 定义3种消息类型
    private static final int START_NUM = 1;   // 开始计算的消息标志
    private static final int ADDING_NUM = 2;  // 计算中的消息标志
    private static final int ENDING_NUM = 3;  // 计算完成的消息标志

    private Handler numHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case START_NUM:
                    // 显示进度条
                    binding.progressBar.setVisibility(View.VISIBLE);
                    binding.progressBar.setProgress(0);
                    break;
                case ADDING_NUM:
                    // 更新进度条的值、更新文本框的内容(msg.arg1)
                    binding.progressBar.setProgress(msg.arg1);
                    binding.tvMsg.setText("进度为" + msg.arg1 + "%");
                    break;
                case ENDING_NUM:
                    // 隐藏进度条、更新文本框的内容
                    binding.progressBar.setVisibility(View.GONE);
                    binding.tvMsg.setText("计算结果：" + msg.arg1);
                    numHandler.removeCallbacks(numThread);
                    break;
            }
        }
    };
    private ActivityMainBinding binding;
    private Thread numThread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int result = 0;
                        numHandler.sendEmptyMessage(START_NUM);// 发送空消息
                        for (int i = 0; i < 101; i++) {
                            try {
                                Thread.sleep(100);
                                result += i;
                            } catch (InterruptedException e) {
                                e.printStackTrace(); return;
                            }
                            if (i % 5 == 0) {
                                // 设置Message的属性值，并发送消息
                                Message msg = Message.obtain();
                                msg.what = ADDING_NUM;
                                msg.arg1 = i;
                                numHandler.sendMessage(msg);
                                // numHandler.obtainMessage(ADDING_NUM, i).sendToTarget();
                            }
                        }
                        Message msg = Message.obtain();
                        msg.what = ENDING_NUM;
                        msg.arg1 = result;
                        numHandler.sendMessage(msg);
                    }
                });
                numThread.start();
            }
        });
    }
}
